package com.cdut.logistics.action.datacentre;

import java.util.ArrayList;
import java.util.List;

import com.cdut.logistics.intercepter.system.AuthorityIntercepter;
import com.cdut.logistics.model.dao.datacentre.User;
import com.cdut.logistics.service.impl.datacentre.UserImpl;
import com.cdut.logistics.service.interf.datacentre.UserService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

/**  
 * @ClassName: UserController  
 * @Description: 用户管理控制类
 * @author wxt.xia
 * @date 2018年1月26日 下午3:28:25    
 */
//@Before(AuthorityIntercepter.class)
public class UserController extends Controller {
	
	private UserService userService = new UserImpl();
	
	/*
	 * 用户查询，可以多重筛选  需要修改*****************************************
	 */
	
	/**
	 * 
	 * @Title: queryById  
	 * @Description: 根据用户id查询用户
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void queryById(){
		/*
		 * 解析json
		 */
		int user_id = Integer.valueOf((String) getPara("user_id"));
		Record user = new Record();
		try {
		    user = userService.queryById(user_id);
			System.out.println(user.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(user);
	}
	
	/**
	 * 
	 * @Title: query  
	 * @Description: 根据用户名称查询用户,如果查询所有人，则name = "%"
	 * @author wxt.xia
	 * @param json
	 * @throws
	 */
	public void query(){
		/*
		 * 解析json
		 */
		String user_name = getPara("user_name")+"%";
		List<Record> userList = new ArrayList<Record>();
		try {
			userList = userService.query(user_name);
		} catch (Exception e) {
			// TODO: handle exception
		}
		renderJson(userList);
	}
	
	/**
	 * 
	 * @Title: add  
	 * @Description: 新增用户
	 * @author wxt.xia
	 * @throws
	 */
	public void add(){
		/*
		 * 解析json
		 */
		
		//下面是测试数据，id需设置为自增长
		User user = new User();
		user.set("name", "冯利");
		user.set("identity", "20155161456156");
		user.set("student_id", "212314215213123");
		user.set("school_id", 1);
		user.set("academy_id", 1);
		user.set("dorm_id", 1);
		user.set("major_id", 1);
		user.set("sex", 1);
		user.set("Rep_id", 1);
		user.set("role_id", 1);
		
		try {
			System.out.println("控制类*************");
			userService.add(user);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: form  
	 * @Description: 修改用户
	 * @author wxt.xia
	 * @throws
	 */
	public void modify(){
		/*
		 * 解析json
		 */
		User user = new User();
		user.set("name", "李姿");
		user.set("sex", 1);
		user.set("id", 3);
		User dataUser = userService.get(user.getId());
		dataUser.setName(user.getName());
		dataUser.setSex(user.getSex());
		try {
			userService.modify(dataUser);
		} catch (Exception e) {
			
		}
	}
	
	/**
	 * 
	 * @Title: remove  
	 * @Description: 删除用户
	 * @author wxt.xia
	 * @throws
	 */
	public void remove(){
		int id = 3;
		try {
			userService.remove(id);
		} catch (Exception e) {
			
		}
	}
}
